/*
 * Copyright (c) 2015, the Dart project authors.
 *
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vm.element

// This file is generated by the script: pkg/vm_service/tool/generate.dart in dart-lang/sdk.

import com.google.gson.JsonArray
import com.google.gson.JsonObject

/**
 * {@link ClassObj} 提供有关 Dart 语言类的信息。
 */
class ClassObj(json: JsonObject) : Obj(json) {

    /**
     * 类最终确定期间发生的错误（如果存在）。
     *
     * 可以返回 <code>null</code>。
     */
    fun getError(): ErrorRef? {
        val obj = json.get("error")?.asJsonObject ?: return null
        val type = json.get("type")?.asString
        if ("Instance" == type || "@Instance" == type) {
            val kind = json.get("kind")?.asString
            if ("Null" == kind) return null
        }
        return ErrorRef(obj)
    }

    /**
     * A list of fields in this class. Does not include fields from superclasses.
     */
    fun getFields(): ElementList<FieldRef> {
        return object : ElementList<FieldRef>(json.get("fields").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): FieldRef {
                return FieldRef(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * A list of functions in this class. Does not include functions from superclasses.
     */
    fun getFunctions(): ElementList<FuncRef> {
        return object : ElementList<FuncRef>(json.get("functions").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): FuncRef {
                return FuncRef(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * A list of interface types for this class.
     *
     * The values will be of the kind: Type.
     */
    fun getInterfaces(): ElementList<InstanceRef> {
        return object : ElementList<InstanceRef>(json.get("interfaces").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): InstanceRef {
                return InstanceRef(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * Is this a base class?
     */
    fun getIsBaseClass(): Boolean {
        return getAsBoolean("isBaseClass")
    }

    /**
     * Is this a final class?
     */
    fun getIsFinal(): Boolean {
        return getAsBoolean("isFinal")
    }

    /**
     * Is this an interface class?
     */
    fun getIsInterfaceClass(): Boolean {
        return getAsBoolean("isInterfaceClass")
    }

    /**
     * Is this a mixin class?
     */
    fun getIsMixinClass(): Boolean {
        return getAsBoolean("isMixinClass")
    }

    /**
     * Is this a sealed class?
     */
    fun getIsSealed(): Boolean {
        return getAsBoolean("isSealed")
    }

    /**
     * The library which contains this class.
     */
    fun getLibrary(): LibraryRef {
        return LibraryRef(json.get("library").asJsonObject)
    }

    /**
     * The location of this class in the source code.
     *
     * Can return <code>null</code>.
     */
    fun getLocation(): SourceLocation? {
        val obj = json.get("location")?.asJsonObject ?: return null
        val type = json.get("type")?.asString
        if ("Instance" == type || "@Instance" == type) {
            val kind = json.get("kind")?.asString
            if ("Null" == kind) return null
        }
        return SourceLocation(obj)
    }

    /**
     * The mixin type for this class, if any.
     *
     * The value will be of the kind: Type.
     *
     * Can return <code>null</code>.
     */
    fun getMixin(): InstanceRef? {
        val obj = json.get("mixin")?.asJsonObject ?: return null
        return InstanceRef(obj)
    }

    /**
     * The name of this class.
     */
    fun getName(): String {
        return getAsString("name")!!
    }

    /**
     * A list of subclasses of this class.
     */
    fun getSubclasses(): ElementList<ClassRef> {
        return object : ElementList<ClassRef>(json.get("subclasses").asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): ClassRef {
                return ClassRef(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * The superclass of this class, if any.
     *
     * Can return <code>null</code>.
     */
    fun getSuperClass(): ClassRef? {
        val obj = json.get("super")?.asJsonObject ?: return null
        val type = json.get("type")?.asString
        if ("Instance" == type || "@Instance" == type) {
            val kind = json.get("kind")?.asString
            if ("Null" == kind) return null
        }
        return ClassRef(obj)
    }

    /**
     * The supertype for this class, if any.
     *
     * The value will be of the kind: Type.
     *
     * Can return <code>null</code>.
     */
    fun getSuperType(): InstanceRef? {
        val obj = json.get("superType")?.asJsonObject ?: return null
        return InstanceRef(obj)
    }

    /**
     * Are allocations of this class being traced?
     */
    fun getTraceAllocations(): Boolean {
        return getAsBoolean("traceAllocations")
    }

    /**
     * The type parameters for the class.
     *
     * Provided if the class is generic.
     *
     * Can return <code>null</code>.
     */
    fun getTypeParameters(): ElementList<InstanceRef>? {
        val typeParameters = json.get("typeParameters") ?: return null

        return object : ElementList<InstanceRef>(typeParameters.asJsonArray) {
            override fun basicGet(array: JsonArray, index: Int): InstanceRef {
                return InstanceRef(array.get(index).asJsonObject)
            }
        }
    }

    /**
     * Is this an abstract class?
     */
    fun isAbstract(): Boolean {
        return getAsBoolean("abstract")
    }

    /**
     * Is this a const class?
     */
    fun isConst(): Boolean {
        return getAsBoolean("const")
    }
}
